package com.bluesky.android.utils;

import java.util.ArrayList;
import java.util.List;

import com.bluesky.android.webservice.MangaChapter;
import com.bluesky.android.webservice.MangaStory;

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.util.Log;
import android.webkit.ValueCallback;

public class MangaDB {
	private SQLiteDatabase db; 
	private final Context context; 
	private final MangaDBHelper dbhelper; 
	public MangaDB(Context c){
		context = c;
		dbhelper = new MangaDBHelper(context, MangaContants.DATABASE_NAME, null, 
					MangaContants.DATABASE_VERSION);
	} 
	public void close() 
	{
		db.close(); 
	} 
	
	public void open() throws SQLiteException 
	{
		try { 
			db = dbhelper.getWritableDatabase();
		} catch(SQLiteException ex) {
			Log.v("Open database exception caught", ex.getMessage()); 
			db = dbhelper.getReadableDatabase();
		} 
	}
	
	public MangaStory getMangaStory(int mangaId){
		Cursor c = db.query(MangaContants.MANGA_TABLE_NAME, 
				new String[]{MangaContants.MANGA_ID,
								MangaContants.MANGA_TITLE, 
								MangaContants.MANGA_AUTHOR, 
								MangaContants.MANGA_IMAGE_LINK, 
								MangaContants.MANGA_LINK,
								MangaContants.MANGA_SUMMARY
									},
					MangaContants.MANGA_ID + "=" + mangaId, null, null, null, null);
		if(c != null){
			c.moveToFirst();
			return MangaStory.cursorToMangaStory(c);
		}
		return null;
	}
	
	public int InsertMangaStory(MangaStory mangaStory){
		int mangaId = 0;
		ContentValues value = MangaStory.mangaStoryToContentValue(mangaStory);
		mangaId = (int) db.insert(MangaContants.MANGA_TABLE_NAME, null, value);
		mangaStory.setMangaId(mangaId);
		return mangaId;
	}
	
	public List<MangaStory> getAllMangaStory(){
		Cursor c = null;
		try{
			c = db.query(MangaContants.MANGA_TABLE_NAME, 
					new String[]{MangaContants.MANGA_ID,
									MangaContants.MANGA_TITLE, 
									MangaContants.MANGA_AUTHOR, 
									MangaContants.MANGA_IMAGE_LINK, 
									MangaContants.MANGA_LINK,
									MangaContants.MANGA_SUMMARY
										},
						null, null, null, null, null);
		}catch(Exception e){
			Log.v("DB", e.getMessage());
		}
		List<MangaStory> listManga = new ArrayList<MangaStory>();
		c.moveToFirst();
		while (!c.isAfterLast()) {
			MangaStory mangaStory = MangaStory.cursorToMangaStory(c);
			listManga.add(mangaStory);
			c.moveToNext();
		}
		return listManga;
	}
	
	public MangaChapter getMangaChapter(int mangaChapterId){
		Cursor cursor = db.query(MangaContants.CHAPTER_TABLE_NAME, new String[]{MangaContants.CHAPTER_ID,
				MangaContants.CHAPTER_DATE, 
				MangaContants.CHAPTER_LINK, 
				MangaContants.CHAPTER_MANGA_ID_FK, 
				MangaContants.CHAPTER_TITLE,
				MangaContants.CHAPTER_CURRENT_IMAGE_POSITION,
				MangaContants.CHAPTER_IS_DOWNLOADING,
				MangaContants.CHAPTER_SAVE_ON_DISK							
					},
					MangaContants.CHAPTER_ID + "=" + mangaChapterId, null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
			return MangaChapter.cursorToMangaChapter(cursor);
		}
		
		return null;
	}
	
	public int InsertMangaChapter(MangaChapter mangaChapter){
		int chapterId = 0;
		ContentValues value = MangaChapter.mangaChapterToContentValue(mangaChapter);
		chapterId = (int) db.insert(MangaContants.CHAPTER_TABLE_NAME, null, value);
		mangaChapter.setChapterId(chapterId);
		return chapterId;
	}
	public void updateManagaChapter(MangaChapter mangaChapter){
		ContentValues value = MangaChapter.mangaChapterToContentValue(mangaChapter);
		db.update(MangaContants.CHAPTER_TABLE_NAME, value, MangaContants.CHAPTER_ID + "=" + mangaChapter.getChapterId(), null);
	}
	public List<MangaChapter> getAllChaptersByMangaId(int mangaId){
		List<MangaChapter> listReturn = new ArrayList<MangaChapter>();
		Cursor cursor = db.query(MangaContants.CHAPTER_TABLE_NAME, new String[]{MangaContants.CHAPTER_ID,
								MangaContants.CHAPTER_DATE, 
								MangaContants.CHAPTER_LINK, 
								MangaContants.CHAPTER_MANGA_ID_FK, 
								MangaContants.CHAPTER_TITLE,
								MangaContants.CHAPTER_CURRENT_IMAGE_POSITION,
								MangaContants.CHAPTER_IS_DOWNLOADING,
								MangaContants.CHAPTER_SAVE_ON_DISK							
									},
									MangaContants.CHAPTER_MANGA_ID_FK + "=" + mangaId, null, null, null, MangaContants.CHAPTER_ID + " desc");
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			MangaChapter mangaChapter = MangaChapter.cursorToMangaChapter(cursor);
			listReturn.add(mangaChapter);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return listReturn;
	}
	
	public List<String> getChapterDataByChapterId(int chapterId){
		List<String> list = new ArrayList<String>();
		Cursor cursor = null;
		try{
			cursor = db.query(MangaContants.IMAGE_TABLE, new String[]{ MangaContants.IMAGE_LINK},
						MangaContants.IMAGE_CHAPTER_ID_FK + "=" + chapterId, null, null, null, MangaContants.IMAGE_INDEX + " asc");
		}catch(Exception ex){
			ex.printStackTrace();
		}		
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			String link = cursor.getString(0);
			list.add(link);
			cursor.moveToNext();
		}
		return list;
	}
	
	public void insertChapterData(String link, int chapterId, int index){
		ContentValues value = new ContentValues();
		value.put(MangaContants.IMAGE_LINK, link);
		value.put(MangaContants.IMAGE_CHAPTER_ID_FK, chapterId);	
		value.put(MangaContants.IMAGE_INDEX, index);
		db.insert(MangaContants.IMAGE_TABLE, null, value);				
	}
	
	public void deleteChapterData(int chapterId){
		db.delete(MangaContants.IMAGE_TABLE, MangaContants.IMAGE_CHAPTER_ID_FK + "=" + chapterId , null);
	}
}
